﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EyFramework.ORM;
using SqlSugar;
using YbService.Entity.Dto;
using YbService.Entity.Hzyb;

namespace YbService.Core.Service
{
    public class PublicService
    {
        private static BaseDaoHis<object> baseDaoHis = new BaseDaoHis<object>();

        public static string GetSysDate(string formatStr)
        {
            return baseDaoHis.GetCurrentDate(formatStr);
        }

        /// <summary>
        /// 保存杭州医保调用入参日志
        /// </summary>
        /// <param name="logInData"></param>
        /// <returns></returns>
        public static long SaveHzybInLog(HzybLogInData logInData)
        {
            var pJiuZhenLsh = new SugarParameter("@MZZYLSH", logInData.MzzyLsh);
            var pRuCan = new SugarParameter("@RUCAN", logInData.RuCan);
            var pJiaoYiLx = new SugarParameter("@JIAOYILX", logInData.JiaoYiLx);
            var pJiaoYiLxmc = new SugarParameter("@JIAOYILXMC", logInData.JiaoYiLxmc);
            var pYongHuId = new SugarParameter("@YONGHUID", logInData.YongHuId);
            var pYeWuZqh = new SugarParameter("@YEWUZQH", logInData.YeWuZqh);
            var pYiYuanJylsh = new SugarParameter("@YIYUANJYLSH", logInData.YiYuanJylsh);
            var pKaLeiXing = new SugarParameter("@KALEIXING", logInData.KaLeiXing);
            var pLianJiBz = new SugarParameter("@LIANJIBZ", logInData.LianJiBz);
            var pLogId = new SugarParameter(":LOGID", null, true);
            var reslt = baseDaoHis.Db.Ado.UseStoredProcedure().GetDataTable("PKG_YBLOG.HZYB_LOG_IN",
                pJiuZhenLsh,
                pRuCan,
                pJiaoYiLx,
                pJiaoYiLxmc,
                pYongHuId,
                pYeWuZqh,
                pYiYuanJylsh,
                pKaLeiXing,
                pLianJiBz,
                pLogId
                );
            return long.Parse(pLogId.Value.ToString());
        }

        /// <summary>
        /// 保存返回日志
        /// </summary>
        /// <param name="logOutData"></param>
        public static void SaveHzybOutLog(HzybLogOutData logOutData)
        {
            var prcParams = baseDaoHis.Db.Ado.GetParameters(new
            {
                LOGID = logOutData.LOGID,
                CHUCAN = logOutData.CHUCAN,
                JIAOYIZT = logOutData.JIAOYIZT
            }).ToList();
            baseDaoHis.Db.Ado.UseStoredProcedure().ExecuteCommand("PKG_YBLOG.HZYB_LOG_OUT", prcParams);
        }

        public static List<DzpzBarInfo> GetDzpzBarInfos(DzpzBarInfoParam dzpzBarInfoParam)
        {
            string sql = @"SELECT A.BINGRENID,
                                b.PAT_NM,
                                b.SEX,
                                C.COMPANY,
                                C.DEPARTMENT,
                                A.DAYINSJ,
                                A.TIAOMAHAO,
                                (SELECT ZHIGONGXM
                                    FROM HIS6.GY_ZHIGONGXX
                                WHERE ZHIGONGID = A.CAOZUOYUAN)
                                CAOZUOYUAN
                                    FROM hzyb_dzpz_dyjl a, ncov_order_record b, ncov_users c
                                WHERE a.bingrenid = b.cht_id
                                AND a.tiaomahao = b.bar_no
                                AND b.pat_seq = c.pat_seq";
            if (!string.IsNullOrEmpty(dzpzBarInfoParam.DAYINRQ))
            {
                sql = sql + string.Format(
                    " AND A.DAYINSJ BETWEEN TO_DATE('{0} 00:00:00','YYYY-MM-DD HH24:MI:SS') AND  TO_DATE('{0} 23:59:59','YYYY-MM-DD HH24:MI:SS') ", dzpzBarInfoParam.DAYINRQ);
            }

            if (!string.IsNullOrEmpty(dzpzBarInfoParam.XINGMING))
            {
                sql = sql + string.Format(" AND C.PAT_NM LIKE '%{0}%'", dzpzBarInfoParam.XINGMING);
            }

            sql = sql + " order by a.DAYINSJ";
            return baseDaoHis.Db.Ado.SqlQuery<DzpzBarInfo>(sql);
        }
    }
}